package org.ee.core.handler;

import org.ee.core.api.Result;
import org.ee.core.constant.ResultCode;
import org.ee.core.handler.exception.BadRequestException;
import org.ee.core.handler.exception.BusinessException;
import org.ee.core.handler.exception.EntityExistException;
import org.ee.core.handler.exception.EntityNotFoundException;
import org.ee.core.util.LogUtil;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 *
 * @Author jokr
 * @Date 2025/8/18 下午3:51
 */
@RestControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Result<?> handleException(Exception e) {
        LogUtil.error("Exception", e);
        e.printStackTrace();
        return Result.failure(ResultCode.FAILURE, e.getMessage());
    }

    @ExceptionHandler(RuntimeException.class)
    public Result<?> handleRuntimeException(RuntimeException e) {
        e.printStackTrace();
        return Result.failure(ResultCode.FAILURE, e.getMessage());
    }

    @ExceptionHandler(BusinessException.class)
    public Result handleBusinessException(BusinessException e) {
        e.printStackTrace();
        return Result.failure(ResultCode.FAILURE, e.getMessage());
    }

    @ExceptionHandler(BadRequestException.class)
    public Result handleBadRequestException(Exception e) {
        e.printStackTrace();
        return Result.failure(ResultCode.VALIDATE_FAILED, e.getMessage());
    }

    @ExceptionHandler(EntityExistException.class)
    public Result handleEntityExistException(Exception e) {
        e.printStackTrace();
        return Result.failure(ResultCode.FAILURE, e.getMessage());
    }

    @ExceptionHandler(EntityNotFoundException.class)
    public Result handleEntityNotFoundException(Exception e) {
        e.printStackTrace();
        return Result.failure(ResultCode.FAILURE, e.getMessage());
    }
}
